
package huajiu.com.serviceImpl;

import huajiu.com.base.Base64;
import huajiu.com.base.CdResult;
import huajiu.com.base.DigitalConvertString;
import huajiu.com.base.ImgApiResponse;
import huajiu.com.service.ImgApiService;
import huajiu.com.utlis.NetHttpRrequest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

/**
 * @author ItBotPjq
 *2018年2月9日
 */
@Service
public class ImgApiServiceImpl implements ImgApiService{
	
	
	public static final Logger logger = Logger.getLogger(ImgApiServiceImpl.class);
	
	public static final String CORPOREITY ="cor";
	
	public static final String TONGUE ="ton";
	
	public static final String EMOTION ="emo";

	/* (non-Javadoc)
	 * @see huajiu.com.service.ImgApiService#getCor(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public ImgApiResponse getCor(HttpServletRequest request) {
		
		return getApiResponse(request,CORPOREITY);
	}


	/* (non-Javadoc)
	 * @see huajiu.com.service.ImgApiService#getTon(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public ImgApiResponse getTon(HttpServletRequest request) {
		
		return getApiResponse(request,TONGUE);
	}


	/* (non-Javadoc)
	 * @see huajiu.com.service.ImgApiService#getEmo(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public ImgApiResponse getEmo(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return getApiResponse(request,EMOTION);
	}
	/**
	 * 
	 * @param request
	 * @return
	 */
	public ImgApiResponse getApiResponse(HttpServletRequest request,String requestMenthondName){
		ImgApiResponse response = new ImgApiResponse();
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);//判断是否是表单文件类型
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload sfu = new ServletFileUpload(factory);
		CdResult cdResult = null;
		String base64 ="";
		List items = new ArrayList();
		String dirFile = "D:/home/wechat/";
		File file = new File(dirFile);
 		if(!file.exists()){
 			file.mkdir();
 		}
		try {
			items = sfu.parseRequest(request);
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//从request得到所有上传域的列表
		for(Iterator iter = items.iterator();iter.hasNext();){
			FileItem fileitem =(FileItem) iter.next();
				if(!fileitem.isFormField() && fileitem != null){//判读不是普通表单域即是file 操作 fileitem文件步骤，可以获取大小、路径
					BufferedInputStream in;
					try {
						in = new BufferedInputStream(fileitem.getInputStream());
						BufferedOutputStream out = new BufferedOutputStream(
								new FileOutputStream(new File(dirFile + "\\"
								+ fileitem.getName())));
						base64 = Base64.encode(in);
						cdResult=getCdResult(requestMenthondName,base64);
						System.out.println(base64);
						//System.out.println(base65);
						out.close();
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
		}
		if(EMOTION.equals(requestMenthondName)){
			response.setData(DigitalConvertString.QXdigitalConvertString(cdResult.getLabel()));
		}else{
			response.setData(DigitalConvertString.TZdigitalConvertString(cdResult.getLabel()));
		}
		response.setResCode(cdResult.getErrorCode().toString());
		response.setResMessage(cdResult.getErrorMessage().toString());
		return response;
	}
	
	public CdResult getCdResult(String requestMenthondName,String base64){
		if(CORPOREITY.equals(requestMenthondName)){
			return NetHttpRrequest.recognizePhysique(base64, "");
		}else if (TONGUE.equals(requestMenthondName)) {
			return NetHttpRrequest.recognizeTongue(base64, "");
		}else if (EMOTION.equals(requestMenthondName)) {
			return NetHttpRrequest.emotion(base64, "");
		}
		return null;
	}

}
